//
//  HomeViewModel.m
//  DingDongXiChe
//
//  Created by wtjr on 2018/1/4.
//  Copyright © 2018年 xieyingze. All rights reserved.
//

#import "HomeViewModel.h"

@implementation HomeViewModel

#pragma mark - Init

- (void)initialize {
    @weakify(self);
    [self.queryNearbyServiceInfoCommand.executionSignals.switchToLatest subscribeNext:^(NSString *resultString) {
        @strongify(self);
        [self handleAnnotationModelResultString:resultString];
    }];
}

#pragma mark - Method

- (void)handleAnnotationModelResultString:(NSString *)resultString {
    [self.annotationModelLists removeAllObjects];
    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:[resultString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:nil];
    for (NSDictionary *dic in dictionary[@"serviceList"]) {
        ServiceModel *model = [[ServiceModel alloc] initWithDictionaty:dic];
        model.currentCoordiante = self.currentCoordiante;
        [self.annotationModelLists addObject:model];
    }
    [self.updateMAPointAnnotationSubject sendNext:self.annotationModelLists];
}

#pragma mark - Interface

- (ServiceModel *)getAnnotationModelWithTitle:(NSString *)title {
    for (ServiceModel *model in self.annotationModelLists) {
        if ([model.service_title isEqualToString:title]) {
            return model;
        }
    }
    return nil;
}

#pragma mark - Setter


#pragma mark - Getter

- (RACSubject *)updateLocationSubject {
    if (!_updateLocationSubject) {
        _updateLocationSubject = [RACSubject subject];
    }
    return _updateLocationSubject;
}

- (RACSubject *)updateMenuViewLayoutSubject {
    if (!_updateMenuViewLayoutSubject) {
        _updateMenuViewLayoutSubject = [RACSubject subject];
    }
    return _updateMenuViewLayoutSubject;
}

- (RACSubject *)gotoServiceDetailSubject {
    if (!_gotoServiceDetailSubject) {
        _gotoServiceDetailSubject = [RACSubject subject];
    }
    return _gotoServiceDetailSubject;
}

- (RACSubject *)updateMAPointAnnotationSubject {
    if (!_updateMAPointAnnotationSubject) {
        _updateMAPointAnnotationSubject = [RACSubject subject];
    }
    return _updateMAPointAnnotationSubject;
}

- (RACSubject *)updateCurrentLocationSubject {
    if (!_updateCurrentLocationSubject) {
        _updateCurrentLocationSubject = [RACSubject subject];
    }
    return _updateCurrentLocationSubject;
}

- (NSMutableArray *)annotationModelLists {
    if (!_annotationModelLists) {
        _annotationModelLists = [NSMutableArray array];
        //测试数据
//        for (int index = 0; index < 10; index ++) {
//            AnnotationModel *model = [AnnotationModel new];
//            model.imageUrl = @"http://f12.baidu.com/it/u=231828582,1274358186&fm=72";
//            model.title = @"南山后海丰田4s点";
//            model.subTitle = @"广东省深圳市南山区海德二道";
//            [_annotationModelLists addObject:model];
//        }
    }
    return _annotationModelLists;
}

/**
 登录请求信号
 */
- (RACCommand *)queryNearbyServiceInfoCommand {
    if (!_queryNearbyServiceInfoCommand) {
        _queryNearbyServiceInfoCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                
                NetworkParam *param = [NetworkParam instance];
                param.methodName = kURL_serviceInfo;
                param.isSHowLoading = NO;
                param.paramArr = [NSMutableArray arrayWithArray:@[
                                                                  kMethod_param(@"user_uuid", @"13424485402")
                                                                  ]];
                [self.request networkRequestWithNetworkParam:param completed:^(BOOL isSucess, NSString *resultString) {
                    if (isSucess) {
                        [subscriber sendNext:resultString];
                        [subscriber sendCompleted];
                    } else {
                        [subscriber sendError:[NSError errorWithDomain:resultString code:0 userInfo:nil]];
                        [self showAlertViewWithMessage:resultString];
                        [subscriber sendCompleted];
                    }
                }];
                return nil;
            }];
        }];
    }
    return _queryNearbyServiceInfoCommand;
}

@end
